package com.jackrain.clouddata.oms.dao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.core.db.Tools;
import com.jackrain.nea.web.query.QueryEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
/*
created by tangzhipeng on 2017/8/16
 */
@Slf4j
@Component
public class TransferDao {

    @Transactional
    public void insert(JSONObject mainJson, JSONArray itemArray, Long clientId,String tableName) {
        log.info("==================调用Transferdao=================");

//        String tableName = mainJson.getString("name");
        String billno = mainJson.getString("BILLNO");
        String countSql = "SELECT COUNT(1) FROM " + tableName + " WHERE AD_CLIENT_ID=" + clientId + " AND BILLNO=?";
        int count = QueryEngine.getInstance().doQueryInt(countSql, new Object[]{billno});
        if (count == 0) {
            StringBuffer strBuf = new StringBuffer("INSERT INTO " + tableName + "(");
            List paramList = new ArrayList();
            Set<String> keySet = mainJson.keySet();
            for (String key : keySet) {
                strBuf.append(key + ",");
                if (key.substring(key.length() - 4, key.length()).equals("DATE")) {
                    paramList.add(TypeUtils.castToDate(mainJson.get(key)));
                } else if (null == mainJson.get(key) || "".equals(mainJson.get(key))) {
                    paramList.add("");
                } else {
                    paramList.add(mainJson.get(key));
                }
            }
            strBuf.append("ID,AD_CLIENT_ID) VALUES(");
            for (int j = 0; j < keySet.size(); j++) {
                strBuf.append("?,");
            }
            strBuf.append("?,?)");
            paramList.add(Tools.getSequence(tableName));
            paramList.add(clientId);
            //插入主表
            QueryEngine.getInstance().executeUpdate(strBuf.toString(), paramList.toArray());

            for (int j = 0; j < itemArray.size(); j++) {
                JSONObject itemValueJson = itemArray.getJSONObject(j);
                JSONArray itemValueArray = itemValueJson.getJSONArray("value");
                String itemTableName = itemArray.getJSONObject(j).getString("name");
                for (int k = 0; k < itemValueArray.size(); k++) {
                    JSONObject itemJo=itemValueArray.getJSONObject(k);
                    StringBuffer strItemBuf = new StringBuffer("INSERT INTO " + itemTableName + "(");
                    paramList = new ArrayList();
                    Set<String> itemKeySet = itemValueArray.getJSONObject(j).keySet();
                    for (String itemkey : itemKeySet) {
                        strItemBuf.append(itemkey + ",");
                        if (itemkey.substring(itemkey.length() - 4, itemkey.length()).equals("DATE")) {
                            paramList.add(TypeUtils.castToDate(itemJo.get(itemkey)));
                        } else if (null == itemJo.get(itemkey) || "".equals(itemJo.get(itemkey))) {
                            paramList.add("");
                        } else {
                            paramList.add(itemJo.get(itemkey));
                        }
                    }
                    strItemBuf.append("ID,AD_CLIENT_ID) VALUES(");
                    for (int s = 0; s < itemKeySet.size(); s++) {
                        strItemBuf.append("?,");
                    }
                    strItemBuf.append("?,?)");
                    paramList.add(Tools.getSequence(itemTableName));
                    paramList.add(clientId);
                    QueryEngine.getInstance().executeUpdate(strItemBuf.toString(), paramList.toArray());
                }
            }
        }
    }
}
